﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SalaryChannel
{
    public enum Usage
    {
        Insert, Update
    }

    public partial class AlbumEditDialog : UserControl
    {
        /// <summary>
        /// Definition of completed event.
        /// </summary>
        public event EventHandler<System.ComponentModel.AsyncCompletedEventArgs> CompletedEvent;

        public AlbumEditDialog()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 更新相册的ID
        /// </summary>
        public string AlbumID {set;get;}

        public Usage Operation
        {
            get;
            set;
        }
        private string oldName = string.Empty;
        public string AlbumName
        {
            set
            {
                this.albumName.Text = value;
                oldName = value;
            }
            get
            {
                return this.albumName.Text;
            }
        }
        private string oldDes = string.Empty;
        public string Description
        {
            get
            {
                return albumDescription.Text;
            }
            set
            {
                this.albumDescription.Text = value;
                oldDes = value;
            }
        }

        public string HeaderTitle
        {
            set
            {
                this.HeaderTitleTB.Text = value;
            }
        }

        /// <summary>
        /// 相册的归属渠道ID
        /// </summary>
        public string ChannelID
        {
            get;
            set;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ServiceInstance.ViewService.NewAlbumCompleted += new EventHandler<SalaryChannel.ViewService.NewAlbumCompletedEventArgs>(ViewService_NewAlbumCompleted);
            ServiceInstance.ViewService.UpdateAlbumCompleted += new EventHandler<SalaryChannel.ViewService.UpdateAlbumCompletedEventArgs>(ViewService_UpdateAlbumCompleted);
        }

        void ViewService_UpdateAlbumCompleted(object sender, SalaryChannel.ViewService.UpdateAlbumCompletedEventArgs e)
        {
            string msg = Properties.Resource.TryAgain;
            try
            {

                if (e.Result)
                {
                    msg = Properties.Resource.Succeeded;
                   
                        if (CompletedEvent != null)
                        {
                            CompletedEvent(this, null);
                        }
                        this.Visibility = Visibility.Collapsed;
                }
                else
                {
                  
                }
            }
            catch
            {
            }
            finally
            {
                IsReady = true;
                App.MessageBox.ShowDialog(msg, Title.Information);
               
            }
        }

        void ViewService_NewAlbumCompleted(object sender, SalaryChannel.ViewService.NewAlbumCompletedEventArgs e)
        {
            string msg = Properties.Resource.TryAgain;
            try
            {

                if (e.Result)
                {
                    msg = Properties.Resource.Succeeded;

                }
                
            }
            catch
            {
            }
            finally
            {
                IsReady = true;
                App.MessageBox.ShowDialog(msg, Title.Information);
                if (!IsReady)
                {
                    if (CompletedEvent != null)
                    {
                        CompletedEvent(this, null);
                    }
                    this.Visibility = Visibility.Collapsed;
                }
            }
        }

        private void UserControl_SizeChanged(object sender, SizeChangedEventArgs e)
        {

        }

        bool _isReady = true;
        /// <summary>
        /// 异步控制，当调用异步函数设为false,异步函数结束后设为true;
        /// </summary>
        bool IsReady
        {
            get
            {
                return _isReady;

            }
            set
            {
                _isReady = value;
                this.Cursor = value ? Cursors.Arrow : Cursors.Wait;
            }
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(AlbumName))
            {
                App.MessageBox.ShowDialog(Properties.Resource.InputInforWarning, Title.Warning);
                return;
            }
            if ( this.Operation == Usage.Update && AlbumName == oldName
                && Description == oldDes)
            {
                App.MessageBox.ShowDialog("您没有做任何修改，无法提交",Title.Warning);
                return;
            }
            try
            {
                if (Operation == Usage.Insert)
                {
                    ServiceInstance.ViewService.NewAlbumAsync(AlbumName, Description, ChannelID);
                }
                else if (Operation == Usage.Update)
                {
                    ServiceInstance.ViewService.UpdateAlbumAsync(AlbumName, Description, AlbumID);
                }

                IsReady = false;
            }
            catch {
                App.MessageBox.ShowDialog(Properties.Resource.ConnectServerFailed, Title.Error);
            }

        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

       
    }
}
